package cn.gson.crm.controller.pay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.EmploDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentPayDao;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.po.FollowStudentPo;
import cn.gson.crm.model.po.StudentPayPo;

@WebServlet("/student/search_pay_record")
public class SearchPayRecord extends BaseServlet {
	/**
	 * 查询收款记录
	 */
	private static final long serialVersionUID = 1L;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private StudentPayDao stuPayDao = new StudentPayDao();
	private StudentPoDao poDao = new StudentPoDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String p = request.getParameter("page");
		String stuName = request.getParameter("stuName");
		String empName = request.getParameter("empName");
		String startDate = request.getParameter("startTime");
		String endDate = request.getParameter("endTime");
		Integer empId = null;
		if (empName != null && !empName.trim().isEmpty()) {
			Employee emp = new EmploDao().emploGetByUserName(empName);
			if (emp != null) {
				empId = emp.getId();
			}
		}
		Integer stuId = null;
		FollowStudentPo student = null;
		StudentPayPo studentDataPo = null;
		if (stuName != null && !stuName.trim().isEmpty()) {
			Student stu = new StudentDao().getStudentByName("正常", "已分配", stuName);
			if (stu != null) {
				stuId = stu.getId();
				student = poDao.getStudentInfo(stuId.toString());
				studentDataPo = stuPayDao.getPayRecord(stuId.toString());
			}
		}
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);
		List<StudentPayPo> studentPayList = null;
		Integer totalPage = null;
		if (startDate == null || endDate == null) {
			studentPayList = stuPayDao.getPayList(page, stuId, empId, sdf.parse(getCurrentDayZero()),
					sdf.parse(sdf.format(new Date())));
			totalPage = stuPayDao.getPayTotalPage(stuId, empId, sdf.parse(getCurrentDayZero()),
					sdf.parse(sdf.format(new Date())));
			request.setAttribute("end", sdf.format(new Date()));
			request.setAttribute("start", getCurrentDayZero());
		} else {
			// 当输入员工姓名或学生名字为空时
			if (stuName == null || stuName.trim().isEmpty() && empName == null || empName.trim().isEmpty()) {
				// 只根据日期查询
				if (startDate != null && !startDate.trim().isEmpty()) {
					studentPayList = stuPayDao.getPayList(page, stuId, empId, sdf.parse(startDate), sdf.parse(endDate));
					totalPage = stuPayDao.getPayTotalPage(stuId, empId, sdf.parse(startDate), sdf.parse(endDate));
				}
				// 输入日期为空时，直接返回null值
				else {
					studentPayList = null;
					totalPage = 0;
				}
			}
			// 学生姓名不为空
			else {
				// 没有该学生或员工直接返回null值
				if (stuId == null || empId == null) {
					studentPayList = null;
					totalPage = 0;
				}
				// 存在该学生或员工
				else {
					// 日期存在则根据学员或员工名字精确查询
					if (startDate != null && !startDate.trim().isEmpty()) {
						studentPayList = stuPayDao.getPayList(page, stuId, empId, sdf.parse(startDate),
								sdf.parse(endDate));
						totalPage = stuPayDao.getPayTotalPage(stuId, empId, sdf.parse(startDate), sdf.parse(endDate));
					}
					// 输入日期为空只通过学生姓名和员工名字查询
					else {
						studentPayList = stuPayDao.getPayList(page, stuId, empId);
						totalPage = stuPayDao.getPayTotalPage(stuId, empId);
					}

				}

			}
			studentPayList = stuPayDao.getPayList(page, stuId, empId, sdf.parse(startDate), sdf.parse(endDate));
			totalPage = stuPayDao.getPayTotalPage(stuId, empId, sdf.parse(startDate), sdf.parse(endDate));
			request.setAttribute("start", startDate);
			request.setAttribute("end", endDate);
		}

		request.setAttribute("page", page);
		request.setAttribute("studentPayList", studentPayList);
		request.setAttribute("po", studentDataPo);
		request.setAttribute("student", student);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("totalPage", totalPage);

		forward("pay/search_pay_record.jsp");
	}

}
